﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

/**
 * 图片管理类
 * @author: gr
 * @date:   2014-07-06
 * @func:   进行图片的管理
 * */

namespace CropPic
{
    class PicManager
    {

        //图片文件夹路径
        private string foldPath = null;
        //用于存储图片
        private FileInfo[] fileArray = null;

        private int fileLen = 0;
        private int filePoint = 0;

        public int FileLen{
            set { fileLen = value; }
            get { return fileLen; }
        }


        public int FilePoint
        {
            set { filePoint = value >= fileLen ? (fileLen - 1):value; }
            get { return filePoint; }
        }


        /**
         * 构造函数
         * fun: 扫描文件夹下的所有文件
         * */
        public PicManager(string _foldPath)
        {
            foldPath = _foldPath;
            try 
            { 
                DirectoryInfo drct = new DirectoryInfo(_foldPath);
                fileArray = drct.GetFiles();
                fileLen = fileArray.Length;
                filePoint = 0;
            }
            catch(Exception e){
                throw (e);
            }
        }


        /**
         * 获取当前的图片
         * */
        public Image getPic() 
        {
            FileInfo file = fileArray[filePoint];

            String strFilename = file.FullName;
            if (strFilename == "")
            {
                throw (new Exception("文件名为空"));
            }

            //pbxOriImage.Image = Image.FromFile(strFilename);
            return Image.FromFile(strFilename);
        }


        ///<summary>
        ///<remarks>获取当前文件的全部路径，包括文件名</remarks>
        ///<example>c:\pic\a.png</example>
        ///<returns>string: FullPath</returns>
        /// </summary>
        public string getFullPath()
        {
            return fileArray[filePoint].FullName;
        }


        /// <summary>
        /// <remarks>获取当前文件的全部路径，包括文件名</remarks>
        /// </summary>
        ///<example>a.png</example>
        ///<returns>string: file name</returns>
        
        public string getName()
        {
            return fileArray[filePoint].Name;
        }


        /**
         * filePoint指向下一个文件
         * */
        public bool toNext()
        {
            if (filePoint < fileLen - 1)
            {
                filePoint++;
                return true;
            }
            else
            {
                return false;
            }
        }


        /**
         * filePoint指向上一个文件
         * */
        public bool toPrev()
        {
            if (filePoint > 0)
            {
                filePoint--;
                return true;
            }
            else
            {
                return false;
            }
        }


        /**
         * 判断文件是否已到末尾
         **/
        public bool isFinished()
        {
            return filePoint >= fileLen;
        }

    }
    /*------end of class PicManager---------*/
}
